<?php
namespace app\common\model;

use app\api\controller\User;
use app\index\model\UserModel;
use think\admin\model\SystemFile;
use think\Model;

class JobModel extends Model{


    protected $table = 'boyi_job';
    protected $pk = 'id';

    public $append = [
        'job_status_info',
        'died_line_info'
    ];
    
    const JOB_STATUS_UNKNOW = 0;   // 
    const JOB_WAITE_RECEIVE = 1; // 等待任务接收方确认接单
    const JOB_WAITE_HANDLE  = 2; // 已接单、等待任务接收方处理
    const JOB_WAITE_EXAMINE = 3; // 等待任务发布方审核
    const JOB_STATUS_REJECT = 4; // 任务被驳回
    const JOB_STATUS_SUCCESS = 5; // 任务审核成功
    const JOB_STATUS_CANCELLED = 6; // 任务作废

    const JOB_STATUS_ARR = [
        self::JOB_STATUS_UNKNOW => [
            'info' => '未知状态',
            'button_type' => 'default',
        ],
        self::JOB_WAITE_RECEIVE => [
            'info' => '等待接单',
            'button_type' => 'info',
        ],
        self::JOB_WAITE_HANDLE => [
            'info' => '等待处理',
            'button_type' => 'danger',
        ],
        self::JOB_WAITE_EXAMINE => [
            'info' => '等待审核',
            'button_type' => 'primary',
        ],
        self::JOB_STATUS_REJECT => [
            'info' => '任务被驳回',
            'button_type' => 'danger',
        ],
        self::JOB_STATUS_SUCCESS => [
            'info' => '任务已完成',
            'button_type' => 'success',
        ],
        self::JOB_STATUS_CANCELLED => [
            'info' => '任务已作废',
            'button_type' => 'success',
        ],
    ];


    public static function getJobNoticeTitle($jobStatus){
        $arr = [
            self::JOB_STATUS_UNKNOW => '未知状态',
            self::JOB_WAITE_RECEIVE => '任务接收通知',
            self::JOB_WAITE_HANDLE => '任务处理通知',
            self::JOB_WAITE_EXAMINE => '任务审核通知',
            self::JOB_STATUS_REJECT => '任务被驳回通知',
            self::JOB_STATUS_SUCCESS => '任务完成通知',
            self::JOB_STATUS_CANCELLED => '任务作废通知',
        ];
        return $arr[$jobStatus] ?? '未知状态';
    }

    public function getJobStatusInfoAttr(){
        return self::JOB_STATUS_ARR[$this->status] ?? [
            'info' => '未知状态',
            'button_type' => 'default',
        ];
    }


    public function getDiedLineInfoAttr(){
        
        if($this->diedline == 0){
            return [
                'str' => '未设置截止日期',
                'tag_type' => 'warning',
            ];
        }
        $diff = $this->diedline - time();
        $day = floor($diff / (3600 * 24));
        // 3 天为危险颜色， 7天为警告颜色
        // 逾期为危险颜色
        $result = '剩余' . $day . '天';
        if($day < 0){
    

            $result = '逾期' . abs($day) . '天';
            $tag_type = 'warning';
        }elseif($day <= 3){
            $tag_type = 'warning';
        }elseif($day <= 7){
            $tag_type = 'danger';
        }else{  
            $tag_type = 'success';
        }
        return [
            'str' => $result,
            'tag_type' => $tag_type,
        ];
    }


    // 获取发起人
    public function jobAddUser(){
        return $this->hasOne(UserModel::class, 'id', 'boyi_uid')->cache(true)->field('id,name,avatar');
    }
    // 获取执行人
    public function jobExecUser(){
        return $this->hasOne(UserModel::class, 'id', 'boyi_to_uid')->cache(true)->field('id,name,avatar');
    }

    // 获取关联客户
    public function customer(){
        return $this->hasOne(CustomerModel::class, 'id', 'customer_id');
    }


    public function fileList(){
        return $this->hasMany(SystemFile::class, 'id', 'file_id_list')->field("id,name,xurl");
    }


}